package cn.bouncyslime.graduationProject.nba.controller;

import cn.bouncyslime.graduationProject.nba.dto.RS_PlayerAveragingDto;
import cn.bouncyslime.graduationProject.nba.service.PlayerDataCompareService;
import cn.bouncyslime.graduationProject.nba.service.PlayerDataService;
import cn.bouncyslime.graduationProject.nba.service.PlayerInfoService;
import cn.bouncyslime.graduationProject.nba.util.ResponseData;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * @author 张一卓
 * @date 2020/4/6 22:45
 **/
@Controller
public class PlayerDataCompareController {

    private static final Logger logger = LoggerFactory.getLogger(PlayerDataCompareController.class);

    @Autowired
    PlayerInfoService playerInfoService;

    @Autowired
    PlayerDataCompareService playerDataCompareService;

    @Autowired
    PlayerDataService playerDataService;

    @RequestMapping(value = "toSinglePlayerDataComparePage")
    public String toSinglePlayerDataComparePage() {
        return "singleDataCompare";
    }

    @RequestMapping(value = "toMultiplePlayerDataComparePage")
    public String toMultiplePlayerDataComparePage(ModelMap modelMap){
        List<String> seasonList = playerDataService.listAllSeason();
        modelMap.addAttribute("seasonList", seasonList);
        return "multipleDataCompare";
    }

    @RequestMapping(value = "searchPlayerForCondition")
    public String searchPlayerForCondition(ModelMap modelMap, @RequestParam String name, @RequestParam String role) {
        ResponseData responseData = playerInfoService.queryPlayer(name, role, "");
        modelMap.addAttribute(responseData);
        return "playerListForCondition";
    }

    @RequestMapping(value = "searchPlayerForMultipleCondition")
    public String searchPlayerForMultipleCondition(ModelMap modelMap, @RequestParam String name, @RequestParam String role) {
        ResponseData responseData = playerInfoService.queryPlayer(name, role, "");
        modelMap.addAttribute(responseData);
        return "playerListForMultipleCondition";
    }

    @RequestMapping(value = "searchTopmostPlayerForCondition")
    public String searchTopmostPlayerForCondition(ModelMap modelMap) {
        ResponseData responseData = ResponseData.success(playerInfoService.listTopPlayers());
        modelMap.addAttribute(responseData);
        return "playerListForCondition";
    }

    @RequestMapping(value = "searchTopmostPlayerForMultipleCondition")
    public String searchTopmostPlayerForMultipleCondition(ModelMap modelMap) {
        ResponseData responseData = ResponseData.success(playerInfoService.listTopPlayers());
        modelMap.addAttribute(responseData);
        return "playerListForMultipleCondition";
    }

    @ResponseBody
    @RequestMapping(value = "listAllTopmostPlayer", method = {RequestMethod.POST, RequestMethod.GET})
    public ResponseData listAllTopmostPlayer(){
        ResponseData responseData = ResponseData.success(playerInfoService.listTopPlayers());
        return responseData;
    }

    @ResponseBody
    @RequestMapping(value = "singlePlayerCompare", method = {RequestMethod.POST})
    public ResponseData singlePlayerCompare(ModelMap modelMap, @RequestBody Map<String, Object> map) {
//        logger.debug(map.toString());
        List<String> playerIdList = (List) map.get("playerIdList");
        String seasonType = (String) map.get("seasonType");
        String type = (String) map.get("type");
        String dataType = (String) map.get("dataType");
        ResponseData responseData = playerDataCompareService.singlePlayerCompare(playerIdList, seasonType, type, dataType);
//        logger.debug(responseData.toString());
        return responseData;
    }

    @ResponseBody
    @RequestMapping(value = "multiplePlayerCompare", method = {RequestMethod.POST})
    public ResponseData multiplePlayerCompare(ModelMap modelMap, @RequestBody Map<String, Object> map) {
        logger.debug(map.toString());
        List<String> playerIdList = (List) map.get("playerIdList");
        String seasonType = (String) map.get("seasonType");
        List<String> typeList = (List) map.get("typeList");
        List<String> dataTypeList = (List) map.get("dataTypeList");
        String season = (String) map.get("season");
        ResponseData responseData = playerDataCompareService.multiplePlayerCompare(playerIdList, seasonType, typeList, dataTypeList, season);
        logger.debug(responseData.toString());
        return responseData;
    }
}
